<template>
<view class="body">
  <!-- 搜索城市 -->
  <view class="search-bar">
    <view class="search-row">
      <view class="input-box">
        <image src="/static/images/search.png"></image>
        <input placeholder="请输入城市名称查询" confirm-type="search" @input="inputTap" @confirm="confirmTap" :value="keywords">
        <image src="/static/images/cancel.png" @tap="delTap"></image>
      </view>
      <text @tap="searchCity">搜索</text>
    </view>
  </view>
  <scroll-view class="scroll" scroll-y="true" :scroll-with-animation="true" :scroll-into-view="cityId" v-if="showCity">
    <view v-for="(item, index) in city" :key="index" class="list">
      <view class="city-key" v-if="item.list[0]" :id="item.key">{{item.key}}</view>
      <view class="city-list">
        <view v-for="(city, index2) in item.list" :key="index2" class="city" @tap="chouseCity" :data-citycode="city.citycode" :data-cityname="city.cityname">
          {{city.cityname}}
        </view>
      </view>
    </view>
  </scroll-view>
  <!-- 搜索结果 -->
  <scroll-view scroll-y="true" style="height:100%;" class="searchRes" v-else>
    <view v-if="cityRes!=''">
      <view class="restitle">搜索结果</view>
      <view v-for="(item, index) in cityRes" :key="index" class="fullname" :data-cityname="item.cityname" :data-citycode="item.citycode" @tap="chouseCity">{{item.cityname}}</view>
    </view>
    <view class="no-city" v-if="keywords && cityRes==''">
      <image src="http://imgbdb3.bendibao.com/xcx/202011/13/20201113154017_35111.png"></image>
      <text>没有找到相关城市</text>
    </view>
    <!-- <view >没有找到相关城市</view> -->
  </scroll-view>
  <view class="navi flex col center">
    <view v-for="(item, index) in city" :key="index" :class="item.list ? 'key-list' : ''" @tap="scrollTo" :data-id="item.key">
      <text v-if="item.list">{{item.key}}</text>
    </view>
  </view>

</view>
</template>

<script>
import util from "../../../../utils/util";
const app = getApp();

export default {
  data() {
    return {
      city: [],
      cityId: "",
      showCity: true,
      keywords: "",
      cityRes: [],
      citycode: "",
      cityname: ""
    };
  },

  components: {},
  props: {},
  onLoad: function (options) {
    util.getUrl(app.globalData.url).then(() => {
      this.getCityInfo();
      this.type = options.type;
    });
  },
  onShareAppMessage: function () {},
  methods: {
    getCityInfo() {
      let data = {
        action: "cut_off",
        category: this.type,
        type: "city"
      };
      util.request("zhuanti.php", data, "POST").then(res => {
        if (res.data.state == 1) {
          let cityKey = [];

          for (let i = 65; i <= 90; i++) cityKey.push({
            key: String.fromCodePoint(i),
            list: []
          });

          cityKey.forEach(item => item.list = res.data.data.city[item.key]);
          this.setData({
            city: cityKey
          });
        }
      });
    },

    scrollTo(e) {
      let id = util.getValue("id", e);
      this.setData({
        cityId: id
      });
    },

    // 输入事件
    inputTap(e) {
      let keywords = e.detail.value;
      this.setData({
        keywords,
        showCity: false
      });
      util.cityList(keywords).then(res => {
        this.setData({
          cityRes: res.cityRes
        });
      });
    },

    // 输入框回车搜索事件
    confirmTap(e) {
      let keywords = e.detail.value;
      this.setData({
        keywords,
        showCity: false
      });
      util.cityList(keywords).then(res => {
        this.setData({
          cityRes: res.cityRes
        });
      });
    },

    // 点击搜索
    searchCity() {
      this.setData({
        // keywords,
        showCity: false
      });
      util.cityList(this.keywords).then(res => {
        this.setData({
          cityRes: res.cityRes
        });
      });
    },

    // 输入框删除icon事件
    delTap() {
      this.setData({
        keywords: "",
        showCity: true
      });
    },

    chouseCity(e) {
      let citycode = util.getValue("citycode", e);
      let cityname = util.getValue("cityname", e);
      let curPages = getCurrentPages();
      curPages[curPages.length - 2].setData({
        citycode,
        cityname
      });
      uni.navigateBack({
        delta: 1
      });
    }

  }
};
</script>
<style>
.body {
  position: relative;
  font-size: 28rpx;
  color: #232323;
}

.city-key {
  height: 38rpx;
  padding-left: 40rpx;
  margin-bottom: 20rpx;
  line-height: 38rpx;
  background-color: #F7F7F7;
}

.city-list {
  margin: 0 40rpx;
}

.city {
  padding-bottom: 17rpx;
  margin-bottom: 19rpx;
  border-bottom: 2rpx solid #F0F0F0;
}

.city-list .city:last-child {
  border-bottom: 0;
}

.navi {
  position: fixed;
  right: 15rpx;
  top: 175rpx;
  font-size: 25rpx;
  width: 60rpx;
  line-height: 37rpx;
  text-align: center;
  color: #ff7e2d;
  z-index: 1;
}

.key-list {
  padding: 5rpx;
}

.scroll {
  height: 100vh;
}

/* 搜索城市 */

.search-bar {
  width: 100%;
  height: 110rpx;
  line-height: 100rpx;
  /* margin-top: -100rpx; */
  top: 0;
  box-sizing: border-box;
  overflow: hidden;
  /* background: rgb(59, 63, 68); */
  padding: 20rpx;
  position: relative;
  /* border-bottom: 15rpx solid #eee; */
}

.search-row {
  display: flex;
  align-items: center;
}

.search-row text {
  font-size: 28rpx;
  font-family: PingFang SC;
  font-weight: 500;
  line-height: 40rpx;
  color: rgba(20, 20, 20, 1);
  opacity: 1;
  margin-left: 30rpx;
}

.input-box {
  height: 62rpx;
  background: rgba(238, 238, 238, 1);
  opacity: 1;
  border-radius: 32rpx;
  padding: 5rpx 30rpx;
  display: flex;
  align-items: center;
  font-size: 28rpx;
  flex: 1;
}

.input-box image {
  width: 30rpx;
  height: 30rpx;
  margin: 0 10rpx;
}

.input-box input {
  flex: 1;
  height: 62rpx;
}

/* .search-city {
  position: fixed;
  top: 0;
  z-index: 2;
  padding: 15rpx 30rpx;
  background-color: #ff7e2d;
  display: flex;
  align-items: center;
}

.search-city input {
  border-radius: 10rpx;
  background-color: #fff;
  height: 62rpx;
  flex: 1;
  padding: 0 15rpx;
  font-size: 26rpx;
}

.search-city text {
  color: #fff;
  font-size: 35rpx;
  margin-left: 25rpx;
} */

.list-detail, .list-city {
  width: 100%;
  height: 100%;
  overflow-y: auto;
  background: #fff;
}

.list-detail .item-nav {
  border-bottom: 1rpx solid #eee;
}

.list-detail .title, .history .title {
  font-size: 32rpx;
  color: #000;
  line-height: 45rpx;
}

.list-detail .address, .history .address {
  font-size: 28rpx;
  color: #aaa;
  white-space: normal;
}

/* 搜索结果 */

.searchRes .restitle {
  background: #eee;
  font-size: 25rpx;
  color: #aaa;
  padding: 12rpx 25rpx;
}

.citytext {
  box-sizing: border-box;
  font-size: 30rpx;
  color: #555;
  padding: 25rpx;
  border-bottom: 1rpx solid #eee;
}

.no-city {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  margin-top: 255rpx;
}

.no-city image {
  width: 298rpx;
  height: 226rpx;
}

.no-city text {
  font-size: 28rpx;
  font-family: PingFang SC;
  font-weight: 500;
  line-height: 40rpx;
  color: rgba(167, 166, 166, 1);
  opacity: 1;
  margin-top: 25rpx;
}
/* 城市列表 */

.list-city {
  position: relative;
}

.py {
  box-sizing: border-box;
  background: #eee;
  font-size: 23rpx;
  color: #aaa;
  padding: 10rpx 25rpx;
}

.usual {
  box-sizing: border-box;
  background: #eee;
  padding: 10rpx 25rpx;
  display: flex;
  align-items: center;
  flex-direction: row;
  justify-content: space-between;
}

.usual text {
  font-size: 23rpx;
  color: #aaa;
}

.usual image {
  width: 28rpx;
  height: 28rpx;
}

.fullname {
  box-sizing: border-box;
  font-size: 30rpx;
  color: #555;
  padding: 25rpx;
  border-bottom: 1rpx solid #eee;
}

.city-py {
  position: fixed;
  top: 21%;
  right: 0;
  /* margin-top: -360rpx; */
}

.city-py view {
  font-size: 25rpx;
  width: 60rpx;
  height: 37rpx;
  line-height: 37rpx;
  text-align: center;
  /* color: #555; */
  color: #ff7e2d;
}

.showPy {
  width: 150rpx;
  height: 150rpx;
  background: #fff;
  border-radius: 15rpx;
  line-height: 150rpx;
  text-align: center;
  font-size: 80rpx;
  margin: auto;
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  z-index: 3;
  /* color: #333; */
  color: #ff7e2d;
  box-shadow: 0 0 150rpx rgba(0, 0, 0, 0.35);
}

.hot-city {
  border: 1rpx solid #eee;
  display: inline-block;
  margin: 25rpx 0 0 22rpx;
  padding: 10rpx 0rpx;
  font-size: 25rpx;
  border-radius: 6rpx;
  width: 118rpx;
  text-align: center;
}

</style>